package com.workmanager.controller;

import com.alibaba.fastjson.JSONObject;
import com.workmanager.bean.StudentBean;
import com.workmanager.bean.TeacherBean;
import com.workmanager.service.regService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringArrayPropertyEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Li on 2016/8/31.
 */
@Controller
@RequestMapping("/reg")
public class regController {
    @Autowired
    private regService regService;
    @RequestMapping("/checkPhone")
    public void checkPhone(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String  num = request.getParameter("shoujihao");
        System.out.println(num);
        boolean result=regService.checkNumber(num);
        JSONObject json = new JSONObject();
        if(result){
            json.put("msg","yes");
        }else
            json.put("msg","no");
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/doreg")
    public void doreg(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String tip=request.getParameter("tip");
        System.out.println(tip);

        boolean result=false;
        if("teacher".equals(tip)){
            System.out.println("进入teavchert");
            TeacherBean teacherBean=new TeacherBean();
            teacherBean.setTeacher_name(request.getParameter("xingming"));
            teacherBean.setTeacher_phone(request.getParameter("shoujihao"));
            teacherBean.setTeacher_password(request.getParameter("mima"));
            result=regService.doteacherreg(teacherBean);
        }else{
            StudentBean studentBean=new StudentBean();
            studentBean.setStudent_name(request.getParameter("xingming"));
            studentBean.setStudent_phone(request.getParameter("shoujihao"));
            studentBean.setStudent_password(request.getParameter("mima"));
            result=regService.dostudentreg(studentBean);
        }
        if(result){
            request.setAttribute("msgtitle","注册成功");
            request.setAttribute("msgcontent","注册成功");
            request.setAttribute("msgurl","#");
        }else{
            request.setAttribute("msgtitle","注册失败");
            request.setAttribute("msgcontent","注册失败");
            request.setAttribute("msgurl","#");

        }
        request.getRequestDispatcher("/msg.jsp").forward(request,response);

    }

}
